package com.ambulance.project.websockt;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebSocketUtils {
    public static final Map<String, Session> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();
	// 单用户推送
    public static void sendMessage(Session session, String message) {
        if (session == null) {
            return;
        }
        final RemoteEndpoint.Basic basic = session.getBasicRemote();
        if (basic == null)
        {
            return;
        }
        try {
            basic.sendText(message);
        } catch (IOException e) {
            System.out.println("sendMessage IOException "+ e);
        }
    }
	// 全用户推送
    public static void sendMessageAll(String message) {
        System.out.println("-------------------------------");
        System.out.println(ONLINE_USER_SESSIONS);
        ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, message));
    }
}
